$(function(){
    var name = '',
        time = 0,
        num  = 0,
        success = 0,
        timer = null;

    function updateData(){
        $('.name').text(name);
        $('.time').text(time);
        $('.num').text(num);
    }
    
    
    while( !name ){
        name = prompt("请输入你的名字：");
    }
    updateData();

    timer = setInterval(() => {
        time++;
        updateData();
        if(success == $('.hardware li').length){
            clearInterval(timer);
            confirm(`${name},你的测试结果：耗时${time}秒，失误${num}次。`);
            end();
        }
    },1000);

    function end(){
        $.ajax({
            url: './php/save.php',
            type: 'POST',
            data: {
                name, time, num
            },
            success(data){
                
            }
        })
    }

    $('.hardware img').draggable({
        revert: 'invalid',
        stop: function(){
            num++;
            updateData();
        }
    });

    $('.cpu').droppable({
        accept: ".hardware_cpu",
        drop: function(ev, ui){
            $(this).addClass('active');
            num--;
            success++;
        }
    });
    $('.grphics').droppable({
        accept: ".hardware_grphics",
        drop: function(ev, ui){
            $(this).addClass('active');
            num--;
            success++;
        }
    });
    $('.hdd').droppable({
        accept: ".hardware_hdd",
        drop: function(ev, ui){
            $(this).addClass('active');
            num--;
            success++;
        }
    });
    $('.network').droppable({
        accept: ".hardware_network",
        drop: function(ev, ui){
            $(this).addClass('active');
            num--;
            success++;
        }
    });
    $('.sound').droppable({
        accept: ".hardware_sound",
        drop: function(ev, ui){
            $(this).addClass('active');
            num--;
            success++;
        }
    });
});